import { Component, OnInit, ChangeDetectorRef, Inject, forwardRef } from '@angular/core';
import { path } from '../../../../path.config';
import { AppComponent } from '../../../../app.component';

import { masterService } from '../../../../../lib/master.service';
import { saveVarService } from '../../../../../lib/saveVar.service';
import { NzModalService } from 'ng-zorro-antd';
@Component({
    selector: 'app-click-order-activity-grid',
    templateUrl: './click-order-activity-grid.component.html',
    styleUrls: ['./click-order-activity-grid.component.css'],
    providers: [forwardRef(() => AppComponent)]
})
export class ClickOrderActivityGridComponent implements OnInit {
    // api
    // 表格数据
    public getList: any = path.campaignManage.oneClickActivity.getList;
    // 列表项详细数据
    public getListItem: any = path.campaignManage.oneClickActivity.getListItem;
    // 获取市场活动类型
    private ActivityType: any = path.campaignManage.oneClickActivity.getSettingType;
    // 目前禁用、启用
    private IsEnabled: any = path.campaignManage.oneClickActivity.IsEnabled;
    // 是否缺货推荐页
    private IsStockoutrecommended: any = path.campaignManage.oneClickActivity.IsStockoutrecommend;

    // 列表项数组
    public rows: Array<any> = [];
    // 下拉框选项
    public options: Array<any> = [];
    // 表格数据头部标题
    public columns: Array<any> = [
        { title: '活动id', name: '1' },
        { title: '最后更新时间', name: '1' },
        { title: '活动名称', name: '1' },
        { title: '活动链接', name: '1' },
        { title: '活动类型', name: '1' },
        { title: '是否新客专享', name: '1' },
        { title: '是否缺货推荐页', name: '1' },
        { title: '是否启用', name: '1' },
        { title: '操作', name: '1' }
    ];
    // 当前页码
    public page: any = 1;
    // 一共多少页
    public itemsPerPage: any = 10;
    // 分页最多显示的页数
    public maxSize: number = 5;
    // 总页数计数变化时，事件数等于总页数计数
    public numPages: number = 1;
    // 总共的列表项数
    public totalItems: number = 0;
    // 分页器的文本配置
    public localText = {
        firstBtn: '首页',
        lastBtn: '末页',
        previousBtn: '上一页',
        nextBtn: '下一页'
    }

    public filter = {
        pageSize: this.itemsPerPage,
        pageIndex: this.page,
        activityName: '',
        enabled: ''
    }

    constructor(public _masterService: masterService,
        public cdRef: ChangeDetectorRef,
        public _saveVarService: saveVarService,
        private confirmServ: NzModalService,
        @Inject(forwardRef(() => AppComponent)) public app: AppComponent) { }

    ngOnInit() {
        this._masterService
            .getData(this.ActivityType)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.options = res.data.items;
                    this.onChangeTable();
                } else {
                    this.app.toastError('错误信息：' + res.errorMessage);
                }
            })
        this._masterService.OneClickReload
            .subscribe(() => {
                this.onChangeTable();
            })
    }

    ngOnDestroy() {
        this.cdRef.detach();
    }

    // 渲染表格
    public onChangeTable = (urlObj?, type?) => {
        if (type == 1 && urlObj) {
            // 1为筛选
            this.filter.activityName = urlObj.activityName;
            this.filter.enabled = urlObj.enabled;
        } else if (urlObj) {
            this.filter.pageSize = urlObj.itemsPerPage;
            this.filter.pageIndex = urlObj.page;
        }
        this._masterService
            .getData(this.getList, this.filter)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this.rows = res.data.items;
                    // 活动类型匹配
                    this.rows.forEach((rowVal, i) => {
                        this.options.forEach((optVal, j) => {
                            if (rowVal.activityTypeName == optVal.id) {
                                rowVal.activityType = optVal.name;
                            }
                        })
                        // url
                        rowVal.url = vmallHost+"h5/quickPurchase/index.html?activityId="+rowVal.activityId;
                    })
                    this.totalItems = res.data.pagination.totalCount;
                    this.itemsPerPage = res.data.pagination.pageSize;
                    this.page = parseInt(res.data.pagination.pageIndex);
                    this.cdRef.detectChanges();
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }

    // 打开编辑弹窗
    public openEditModal(rowId) {
        let urlObject = {
            activityId: rowId
        }
        // 获取列表详情
        this._masterService
            .getData(this.getListItem, urlObject)
            .subscribe((res) => {
                if (res.statusCode == 200) {
                    this._saveVarService.clickOrderEdit = res.data;
                    this._masterService.openClickOrder.emit(res.data);
                } else {
                    this.app.toastError('错误信息： ' + res.errorMessage);
                }
            })
    }

    // 启用、禁用按钮
    public IsEnable(id, enabled, activityName) {
        if (enabled == '1') {
            this.confirmServ.confirm({
                nzTitle: '是否停用"'+activityName+'"活动,请确认不在投放中，停用后用户将无法使用',
                nzOnOk() {
                    postIsEnable();
                }
            })
        }
        if (enabled == '0') {
            this.confirmServ.confirm({
                nzTitle: '是否确认启用？',
                nzOnOk() {
                    postIsEnable();
                }
            })
        }

        let postIsEnable = () => {
            let urlObj = {
                activityId: id,
                enabled: !enabled
            }
            this._masterService
                .postData(this.IsEnabled, urlObj)
                .subscribe((res) => {
                    if (res.statusCode == 200) {
                        this.onChangeTable();
                    } else if (res.statusCode == 11000115) {
                        this.confirmServ.confirm({
                            nzTitle: res.errorMessage
                        })
                    } else {
                        this.app.toastError('错误信息：' + res.errorMessage);
                    }
                })
        }
    }

    // 设置缺货推荐页
    public IsStockoutrecommend(id, stockoutRecommend) {
        if (stockoutRecommend == '0') {
            this.confirmServ.confirm({
                nzTitle: '是否设为缺货推荐页？',
                nzOnOk() {
                    postIsStockoutrecommend();
                }
            })
        }
        if (stockoutRecommend == '1') {
            this.confirmServ.confirm({
                nzTitle: '是否取消缺货推荐页？',
                nzOnOk() {
                    postIsStockoutrecommend();
                }
            })
        }
        let postIsStockoutrecommend = () => {
            let urlObj = {
                activityId: id,
                stockoutRecommend: 1 - stockoutRecommend
            }
            this._masterService
                .postData(this.IsStockoutrecommended, urlObj)
                .subscribe((res) => {
                    if (res.statusCode == 200) {
                        this.onChangeTable();
                    } else if (res.statusCode == 11000115) {
                        this.confirmServ.confirm({
                            nzTitle: res.errorMessage
                        })
                    } else {
                        this.app.toastError('错误信息：' + res.errorMessage);
                    }
                })
        }
    }
}
